home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13193 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: noc.netcom.net!news
  2. From: Tarang Deshpande <tarang@willows.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: switching base 10 to hex
  5. Date: Thu, 04 Apr 1996 17:47:36 -0800
  6. Organization: NETCOM Network Operations
  7. Message-ID: <31647BB8.759C@willows.com>
  8. References: <4ju6a9$kl6@coranto.ucs.mun.ca>
  9. NNTP-Posting-Host: daffy.willows.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
  14.  
  15. J.Deeley wrote:
  16. > Hello,
  17. > I read the faq (very interesting!) and also did a search at the Internet
  18. > Public Library (at which I found some other realy neat info) but I am
  19. > still stuck on this. Appologies if this is the wrong group to ask.
  20. > Perhaps you could point me to the right group, or a web site with this
  21. > info. I need?
  22. > I am teaching myself to program in C using a book that came with my
  23. > compiler. In the book I just ran into a small section which deals with
  24. > switching base 10 numbers to base two numbers (no problems there, thats
  25. > easy) and also switching base 10 numbers to hexidecimal (base 16).
  26. > Agh! My book does a lousy job of explaining how that works, and one of
  27. > the alternative explanations I found on the net does no better. Can
  28. > someone give me a good, simple way to get a hexadecimal number from a
  29. > normal number? Like the number 400 for instance? Go slow...
  30. > JD
  31. > --
  32. > Caution...Newbie Programmer on Board!
  33. > --
  34.  
  35.  
  36. I am assuming that you want to know the math behind hex numbers and not
  37. just how to print them out from a C program.  If so read on, otherwise
  38. sorry.
  39.  
  40. First off understand what a number like 400 means in decimal.  It actually
  41. means ( 4 * 100 ) + ( 0 * 10 ) + ( 0 * 1 ) or better yet ( 4 * 10^2 ) +
  42. ( 0 * 10^1 ) + ( 0 * 10^0 ).  In hexidecimal a number is represented in 
  43. exactly the same way except that instead of 10s you use 16s.  So the number
  44. 0x400 ( 400 hex which is not equal 400 decimal ) would be ( 4 * 16^2 ) +
  45. ( 0 * 16^1 ) + ( 0 * 16^0 ).
  46.  
  47. Got that so far?
  48.  
  49. Now the question becomes how do you convert a decimal number to hex.  Using
  50. 400 decimal we want to find the following
  51.  
  52. 400 = ( h1 * 16^n ) + ( h2 * 16^(n-1) ) + ( h3 * 16^(n-2) ) + ... ( hn+1 * 16^0 )
  53.  
  54. note that if n were large ( in this case greater than 2 ) then h1 would be 0 just as
  55. 400 = ( 0 * 10^3 ) + ...
  56.  
  57. So in converting the following algorithm will yield the proper result.
  58.  
  59. find n such that 16^n >= 400 or whatever other number your trying to convert.
  60. In this case n = 3 because 16^3 = 4096 and 4096 > 400.
  61.  
  62. Now how many times does 16^(n-1) = 16^(2) = 256 go into 400, the answer is 1.
  63. What's left over?  The answer is 144.
  64. Now how many times does 16^(n-2) = 16^1 = 16 go into 144, the answer is 9.
  65. What's left over?  The answer is 0.
  66.  
  67. Therefore 400 = ( 1 * 16^2 ) + ( 9 * 16^1 ) + ( 0 * 16^0 ) or 0x190
  68.  
  69. Got it?
  70.